/**
 * Created by yanghl on 2015/4/1.
 */

define(function (require, exports, module) {
  'use strict';

  /**
   * 判断环境 浏览器版本，运行平台
   * @class Common.Env
   */

  var Browser;
  (function (Browser) {
    Browser[Browser["MSIE"] = 0] = "MSIE";
    Browser[Browser["FF"] = 1] = "FF";
    Browser[Browser["SAFARI"] = 2] = "SAFARI";
    Browser[Browser["CHROME"] = 3] = "CHROME";
    Browser[Browser["OPERA"] = 4] = "OPERA";
    Browser[Browser["OTHER"] = 5] = "OTHER";
  })(Browser || (Browser = {}));
  var Os;
  (function (Os) {
    Os[Os["WINDOWS"] = 0] = "WINDOWS";
    Os[Os["IOS"] = 1] = "IOS";
    Os[Os["OSX"] = 2] = "OSX";
    Os[Os["ANDROID"] = 3] = "ANDROID";
    Os[Os["OTHER"] = 4] = "OTHER";
  })(Os || (Os = {}));

  var IOSType;
  (function (Os) {
    IOSType[IOSType["IPHONE"] = 0] = "IPHONE";
    IOSType[IOSType["IPAD"] = 1] = "IPAD";
    IOSType[IOSType["IPOD"] = 2] = "IPOD";
    IOSType[IOSType["OTHER"] = 3] = "OTHER";
  })(IOSType || (IOSType = {}));

  var Env = (function () {
    function Env() {
    }

    /**
     * InternetExplorer 返回true<br />
     * 判断浏览器是否为IE和 IE版本是否相对应
     *
     * @static
     * @method isIE
     * @param {number} version  如果没有指定ie版本，则判断是不是ie浏览器
     * @return {boolean}
     */
    Env.isIE = function (version) {
      if (typeof version === "undefined") {
        version = null;
      }
      if (version === null) {
        return Env.BROWSER === 0 /* MSIE */;
      }
      return Env.BROWSER === 0 /* MSIE */ && Env.VERSION === version;
    };

    /**
     * FireFox浏览器 true
     *
     * @static
     * @method isFF
     * @return {boolean}
     */
    Env.isFF = function () {
      return Env.BROWSER === 1 /* FF */;
    };

    /**
     * Safari浏览器 true
     *
     * @static
     * @method isSafari
     * @return {boolean}
     */
    Env.isSafari = function () {
      return Env.BROWSER === 2 /* SAFARI */;
    };

    /**
     * Chrome浏览器 true
     *
     * @static
     * @method isChrome
     * @return {boolean}
     */
    Env.isChrome = function () {
      return Env.BROWSER === 3 /* CHROME */;
    };

    /**
     * Windows系统 true
     *
     * @static
     * @method isWindows
     * @return {boolean}
     */
    Env.isWindows = function () {
      return Env.OS === 0 /* WINDOWS */;
    };

    /**
     * iOS系统 true
     *
     * @static
     * @method isIOS
     * @return {boolean}
     */
    Env.isIOS = function () {
      return Env.OS === 1 /* IOS */;
    };


    /**
     * OsX系统 true
     *
     * @static
     * @method isOsX
     * @return {boolean}
     */
    Env.isOsX = function () {
      return Env.OS === 2 /* OSX */;
    };

    /**
     * Android系统 true
     *
     * @static
     * @method isAndroid
     * @return {boolean}
     */
    Env.isAndroid = function () {
      return Env.OS === 3 /* ANDROID */;
    };

    /**
     * IPhone true
     *
     * @static
     * @method isIPhone
     * @return {boolean}
     */
    Env.isIPhone = function () {
      return Env.IOSType === 0 /* IPHONE */;
    };

    /**
     * IPad true
     *
     * @static
     * @method isIPad
     * @return {boolean}
     */
    Env.isIPad = function () {
      return Env.IOSType === 1 /* IPAD */;
    };

    /**
     * IPod true
     *
     * @static
     * @method isIPod
     * @return {boolean}
     */
    Env.isIPod = function () {
      return Env.IOSType === 2 /* IPOD */;
    };

    /**
     * 获取IE浏览器版本号
     * @static
     * @method getMSIEVersion
     * @return {Integer}
     */
    Env.getMSIEVersion = getMSIEVersion;

    /**
     * 获取谷歌浏览器版本号
     * @static
     * @method getChromeVersion
     * @return {Integer}
     */
    Env.getChromeVersion = function () {
      if (Env.VERSION === undefined) {
        var matches = navigator.userAgent.toLowerCase().match(/chrome\/([\d.]+)/);
        var version = (matches.length > 1) ? matches[1] : null;
        if (version) {
          Env.VERSION = parseInt(version, 10);
        }
      }
      return Env.VERSION;
    };

    /**
     * 获取火狐浏览器版本号
     * @static
     * @method getFirefoxVersion
     * @return {Integer}
     */
    Env.getFirefoxVersion = function () {
      if (Env.VERSION === undefined) {
        var matches = navigator.userAgent.toLowerCase().match(/firefox\/([\d.]+)/);
        var version = null;
        if (matches != null) {
          version = (matches.length > 1) ? matches[1] : null;
        }
        if (version) {
          Env.VERSION = parseInt(version, 10);
        }
      }
      return Env.VERSION;
    };

    /**
     * IE版本号
     */
    Env.IE6 = 6;
    Env.IE7 = 7;
    Env.IE8 = 8;
    Env.IE9 = 9;
    Env.IE10 = 10;
    Env.IE11 = 11;
    Env.IE12 = 12;

    return Env;
  })();

  // Initialize
  function challengeMSIE(ua) {
    if (ua.indexOf("compatible; MSIE") < 0 && ua.indexOf("Trident/") < 0 && ua.indexOf("IEMobile/")) {
      return false;
    }
    Env.BROWSER = 0 /* MSIE */;
    getMSIEVersion();

    return true;
  }

  function getMSIEVersion() {
    if (Env.VERSION === undefined) {
      var matches = navigator.userAgent.match(/((msie|MSIE)\s|rv:)([\d\.]+)/);
      var version = null;
      if (matches != null) {
        version = (matches.length > 3) ? matches[3] : null;
      }
      if (version) {
        Env.VERSION = parseInt(version, 10);
      }
    }
    return Env.VERSION;
  }

  function challengeFirefox(ua) {
    if (ua.indexOf("Firefox/") < 0) {
      return false;
    }
    Env.BROWSER = 1 /* FF */;
    return true;
  }

  var chromePattern = /(?:Chrome|CrMo|CriOS)\/([.0-9]+)/;
  var operaBlinkPattern = /OPR\/([.0-9]+)/;

  function challengeSafariChrome(ua) {
    if (ua.indexOf("Safari/") < 0)
      return false;
    if (chromePattern.exec(ua)) {
      if (operaBlinkPattern.exec(ua)) {
        Env.BROWSER = 4 /* OPERA */;
        return true;
      }
      Env.BROWSER = 3 /* CHROME */;
      return true;
    }
    Env.BROWSER = 2 /* SAFARI */;
    return true;
  }

  function tryBrowser(userAgent) {
    Env.BROWSER = 5 /* OTHER */;
    if (challengeMSIE(userAgent)) {
      return;
    }
    if (challengeSafariChrome(userAgent)) {
      return;
    }
    if (challengeFirefox(userAgent)) {
      return;
    }
  }

  function challengeWindows(ua) {
    if (ua.indexOf("Windows") < 0) {
      return false;
    }
    Env.OS = 0 /* WINDOWS */;
    return true;
  }

  function challengeOsX(ua) {
    if (ua.indexOf("Mac OS X") < 0) {
      return false;
    }
    Env.OS = 2 /* OSX */;
    return true;
  }

  function challengeIOS(ua) {
    if (ua.indexOf("iPhone") < 0 && ua.indexOf("iPad") < 0 && ua.indexOf("iPod") < 0) {
      return false;
    } else {
      if ((ua.indexOf("iPhone") > 0)) {
        Env.IOSType = 0;
        /* IPHONE */
      } else if ((ua.indexOf("iPad") > 0)) {
        Env.IOSType = 1;
        /* IPAD */
      } else if ((ua.indexOf("iPod") > 0)) {
        Env.IOSType = 2;
        /* IPOD */
      } else {
        Env.IOSType = 3;
        /* OTHER */
      }
    }
    Env.OS = 1 /* IOS */;
    return true;
  }

  function challengeAndroid(ua) {
    if (ua.indexOf("Android") < 0) {
      return false;
    }
    Env.OS = 3 /* ANDROID */;
    return true;
  }

  function tryOs(userAgent) {
    Env.OS = 4 /* OTHER */;
    if (challengeWindows(userAgent)) {
      return;
    }
    if (challengeIOS(userAgent)) {
      return;
    }
    if (challengeOsX(userAgent)) {
      return;
    }
    if (challengeAndroid(userAgent)) {
      return;
    }
  }

  tryBrowser(navigator.userAgent);
  tryOs(navigator.userAgent);


  return Env;
});
